import request from '@/common/request.js'

// 上传文件
export function uploadFile(filePath) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: request.baseUrl + '/api/user/upload',
			filePath: filePath,
			name: 'file',
			header: {
				'token': uni.getStorageSync('token')
			},
			success: (res) => {
				const data = JSON.parse(res.data)
				resolve(data)
			},
			fail: (error) => {
				reject(error)
			}
		})
	})
}

// 更新用户信息
export function updateUserInfo(data) {
	return request.httpTokenRequest({
		url: '/api/user/updateUsers',
		method: 'get',
		data
	})
}

// 获取用户信息并处理响应
export async function getUserInfo() {
	try {
		const response = await request.httpTokenRequest({
			url: '/api/user/getUserInfo',
			method: 'get'
		});

		// 统一处理响应格式
		if (response && response.data.code === 1) {
			// 返回标准化的用户数据
			return {
				...response.data,
				// 确保统计数据默认值
				stats: response.stats || {
					following: 0,
					visitors: 0,
					followers: 0
				}
			};
		}
		throw new Error(response?.msg || '获取用户信息失败');
	} catch (error) {
		console.error('用户信息接口异常:', error);
		throw error; // 向上抛出错误，由调用方处理UI提示
	}
}

// 注销账户
export function deleteAccount() {
	return request.httpTokenRequest({
		url: '/api/user/delete',
		method: 'post'
	})
}

// 获取用户标签全部
export function getUserTags() {
	return request.httpTokenRequest({
		url: '/api/user/getTag',
		method: 'get'
	})
}
// 获取用户动态
export function getUserDynamic(data) {
	return request.httpTokenRequest({
		url: '/api/index/getMyPosts',
		method: 'get',
		data
	})
}

// 获取其他人主页信息
export function getUserProfile(data) {
	return request.httpTokenRequest({
		url: '/api/index/getOtherUserInfo',
		method: 'get',
		data
	})
}

// 关注用户
export function followUser(data) {
	return request.httpTokenRequest({
		url: '/api/index/saveFollow',
		method: 'get',
		data
	})
}

// 获取对话列表
// getChatList
// type 1 私聊 2 群聊
export function getChatList(data) {
	return request.httpTokenRequest({
		url: '/api/index/getChatList',
		method: 'get',
		data
	})
}

// 获取组队成员
export function getTeamMember(data) {
	return request.httpTokenRequest({
		url: '/api/index/getTeamMember',
		method: 'get',
		data
	})
}

// 加入组队
export function joinTeam(data) {
	return request.httpTokenRequest({
		url: '/api/index/joinTeam',
		method: 'GET',
		data
	});
}

// 退出组队
export function exitTeam(data) {
	return request.httpTokenRequest({
		url: '/api/index/exitTeam',
		method: 'GET',
		data
	});
}

// 解散群组
export function disbandTeam(data) {
	return request.httpTokenRequest({
		url: '/api/index/disbandTeam',
		method: 'GET',
		data
	});
}

// 发送消息
export function saveMessage(data) {
	return request.httpTokenRequest({
		url: '/api/index/saveMessage',
		method: 'GET',
		data: data
	});
}

// 获取聊天记录
export function getChatHistory(data) {
	return request.httpTokenRequest({
		url: '/api/index/getMessageList',
		method: 'GET',
		data
	});
}